package com.example.petmall.controller;

import com.example.petmall.entity.Order;
import com.example.petmall.entity.OrderItem;
import com.example.petmall.repository.OrderRepository;
import com.example.petmall.repository.OrderItemRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@CrossOrigin
public class OrderController {
    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private OrderItemRepository orderItemRepository;

    @GetMapping("/orders")
    public List<Order> getOrders(@RequestParam Integer userId) {
        return orderRepository.findByUserId(userId);
    }

    @PostMapping("/order")
    public Order createOrder(@RequestBody Order order) {
        return orderRepository.save(order);
    }

    @GetMapping("/order-items")
    public List<OrderItem> getOrderItems(@RequestParam Integer orderId) {
        return orderItemRepository.findByOrderId(orderId);
    }
}    